package com.example.qsindusty.jmm_spline.Unallcation;

import android.text.TextUtils;

import com.example.qsindusty.jmm_spline.OrdeFragment.OrderPresenter;
import com.example.qsindusty.jmm_spline.api.ApiManager;
import com.example.qsindusty.jmm_spline.api.BaseData;
import com.yixin.ystartlibrary.base.IBaseView;
import com.yixin.ystartlibrary.utils.LogUtils;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administration on 2018/4/2.
 *
 *带分配
 */

public class UnAllcationPresenter extends OrderPresenter implements UnAllcationContract.Presenter {
    UnAllcationContract.View mView ;
    @Override
    public void bindView(IBaseView view) {
        super.bindView(view);
        mView= (UnAllcationContract.View) view;
    }

    @Override
    public void unbindView() {
        super.unbindView();
        dispose();
    }

    @Override
    public void choiceDriver(String token, String ordernum, final String drivernum, final String dirvername, final int postion) {
        ApiManager.getInstence().getJmmApi().choiceDriver(token,ordernum,drivernum,dirvername)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(BaseData orderData) {
                        LogUtils.i(orderData.toString());
                        if (TextUtils.equals(orderData.getStatus(),"0"))
                            mView.choiceDriverSuccessful(orderData.getMessage(),postion,dirvername);
                        else
                            mView.choiceDriverFrair(orderData.getMessage());

                    }

                    @Override
                    public void onError(Throwable e) {
                    mView.closeLoading();
                        mView.choiceDriverFrair(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    mView.closeLoading();
                    }
                });
    }

    @Override
    public void choiceCar(String token, final String ordernum, String jsondata, final String cph, final int position) {
        ApiManager.getInstence().getJmmApi().choiceCar(token,ordernum,jsondata)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseData>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(BaseData orderData) {
                     //   LogUtils.i(orderData.toString());
                        if (TextUtils.equals(orderData.getStatus(),"0"))
                            mView.choiceCarSuccessful(orderData.getMessage(),position,cph);
                    else
                        mView.choiceCarFrair(orderData.getMessage());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.closeLoading();
                        mView.choiceCarFrair(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.closeLoading();
                    }
                });

    }
}
